// Παρακατω δημιουργείται ένα γενικο global αντικείμενο οπου θα βάλουμε
// τα περισσότερα global αντικείμενα της εφαρμογής μας.

var globals = {};

globals.constants = {};
globals.constants.fyiDelay = 15000;

globals.colors = {};
globals.colors.error = '#FF3300';

globals.debug = {};
globals.debug.last = null;

globals.debug.print = function(s) {
	if (notSet(globals.debug.on)) { return; }
	if (!globals.debug.on) { return; }

	var q = getelid('debug');
	if (notSet(q)) { return; }

	q.innerHTML = '<div>' + s + '</div>' + q.innerHTML;
};

globals.misc = {};
globals.misc.setIpsosStilis = function(ipsos) {
	document.getElementById("main").style.height = ipsos + "cm";
	document.getElementById("kafenio").style.height = ipsos + "cm";
	document.getElementById("controlPanel").style.height = ipsos + "cm";
	document.getElementById("tablo").style.height = ipsos + "cm";
	document.getElementById("sizitisi").style.height = ipsos + "cm";
};

globals.misc.setInputColor = function(obj, color) {
	if (!obj.hasOwnProperty('originalColor')) {
		obj.originalColor = obj.style.color;
	}
	obj.style.color = color;
};

globals.misc.resetInputColor = function(obj) {
	if (!obj.hasOwnProperty('originalColor')) {
		return;
	}
	if (obj.style.color == obj.originalColor) {
		return;
	}
	obj.style.color = obj.originalColor;
};

window.onload = function() {
	init();
};

function init() {
	topothetisiMain();
	globals.debug.print('Debug area');
}

function topothetisiMain () {
	var main = getelid('main');
	if (notSet(main)) { return; }
	var x = getelid('toolbar');
	if (notSet(x)) { return; }
	main.style.paddingTop = x.clientHeight + 'px';

	x = getelid('footer');
	if (notSet(x)) { return; }
	main.style.paddingBottom = x.clientHeight + 'px';
}

function newRequest() {
	if (window.XMLHttpRequest) {
		return (new XMLHttpRequest());
	}

	if (window.ActiveXObject) {
		return (new ActiveXObject());
	}

	window.location.href = globals.server + 'error.php?minima=newRequest: fail';
	return null;
}

function openRequest(req, url, async) {
	if (notSet(async)) {
		async = true;
	}

	req.open('POST', globals.server + url + '.php?xyz=abc&qwe=asd', async); 
	req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	req.setRequestHeader("Connection", "close");
	
}

function notSet(x) {
	return ((x === undefined) || (x === null));
}

function isSet(x) {
	return (!notSet(x));
}

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g, "");
};

String.prototype.uri = function() {
	return encodeURIComponent(this);
};

String.prototype.validLogin = function() {
	return this.match(/^[A-Z][A-Z0-9_@.+-]*$/i);
};

String.prototype.validEmail = function() {
	return this.match(/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i);
};

String.prototype.jsonSafe = function() {
	return this.replace(/'/g, "\\'");
};

function fyi(msg) {
	var x = getelid('fyi');
	if (notSet(x)) { return; }

	if (msg == '') { msg = null; }

	if (isSet(msg)) {
		x.innerHTML = msg;
		x.style.visibility = 'visible';
		setTimeout(function() {
			x.style.visibility = 'hidden';
			x.innerHTML = '&nbsp';
		}, globals.constants.fyiDelay);
		return;
	}

	x.innerHTML = '';
	x.style.visibility = 'hidden';
}

function getelid(id) {
	return document.getElementById(id);
}

function uri(s) {
	return encodeURIComponent(s);
}

function imgFail(img, src) {
	try {
		img.src = globals.server + 'images/redX.png';
		setTimeout(function() {
			try { img.src = src; } catch(e) {}
			}, 2000);
	} catch(e) {}
}
